<template>
  <div>
    <!-- accept 限制图片格式，不符合的不显示 -->
    <el-upload
      v-if="!isShowProgress"
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :http-request="onRequest"
      :on-change="onChange"
      :class="{ display: fileList.length }"
      :on-remove="onRemove"
      :on-preview="handlePictureCardPreview"
      :before-upload="beforeUpload"
      accept="image/png, image/jpeg, image/gjf"
    >
      <span>上传图片</span>
    </el-upload>

    <!-- 进度条 -->
    <el-progress v-if="isShowProgress" type="circle" :percentage="progress" />

    <!-- dialog -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID0qPipgFYbO7amiNkqgFWWFiupSqxnMfg',
  SecretKey: 'lFSP5UcT3NQChwub190auwhBOKzMSHWV'
})
export default {
  name: 'UploadImage',
  // props: {
  //   fileList: {
  //     type: Array,
  //     required: true
  //   }
  // },
  data () {
    return {
      fileList: [],
      dialogImageUrl: '', // 点击的图片地址
      dialogVisible: false,
      currentUid: '',
      progress: 0, // 进度条
      isShowProgress: false // 进度条是否显示
    }
  },

  props: {
    value: {
      type: String,
      default: ''
    }
  },

  created () {

  },

  methods: {
    // 删除图片
    onRemove (file) {
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },

    // 自定义上传
    onRequest (data) {
      console.log(cos)
      console.log(data)
      cos.putObject({
        Bucket: 'hrsass-hz-1307760767', /* 必须 桶的名字 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 地址 */
        Key: data.file.name, /* 必须 文件名 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.progress = progressData.percent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        if (err) {
          this.$message.error('上传失败,请重新上传')
          this.isShowProgress = false
          this.fileList = this.fileList.filter(item => item.uid !== this.currentUid)
        }
        if (!err && data.statusCode === 200) {
          const url = 'https://' + data.Location
          const index = this.fileList.findIndex(item => {
            return item.uid === this.currentUid
          })
          this.$emit('input', url)
          this.fileList[index].url = url
          this.$emit('on-success', this.fileList, url)
          setTimeout(() => {
            this.isShowProgress = false
          }, 700)
        }
      })
    },

    // 选择图片时触发
    onChange (file, fileList) {
      this.fileList = fileList
    },

    // 点击预览图片时触发
    handlePictureCardPreview (file) {
      this.dialogVisible = true
      this.dialogImageUrl = file.url
    },

    // 选择图片上传前触发
    beforeUpload (file) {
      // const imageType = ['image/png', 'image/jpeg', 'image/gjf'] // 限制图片格式
      // if (!imageType.includes(file.type)) {
      //   this.$message.error('请选择正确格式的图片')
      //   return false
      // }
      const maxSize = 3 * 1024 * 1024 // 限制1MB
      if (file.size > maxSize) {
        this.$message.error('请选择3MB以内的图片')
        return false
      }
      this.currentUid = file.uid
      this.isShowProgress = true
      return true
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .display .el-upload--picture-card {
  display: none;
}
</style>
